package com.engineering.elink.interceptor;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CORSFilter implements Filter{
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;
		req.setCharacterEncoding("UTF-8");
		res.setCharacterEncoding("UTF-8");

		res.setHeader("Access-Control-Allow-Origin", "*");
		
		res.setHeader("Content-Type", "application/json;charset=UTF-8");
		//res.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8020");
		res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
		res.setHeader("Access-Control-Max-Age", "3600");
//		res.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
//		res.setHeader("Access-Control-Allow-Credentials","true");
		chain.doFilter(request, response);
		
//		res.setHeader("Access-Control-Allow-Origin", "http://115.29.168.57:8080");
//		res.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8020,http://localhost:8080");
//		response.setHeader("Access-Control-Allow-Headers", "Content-Type");
//		response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
	}
	
	
	@Override
	public void destroy() {
		
	}
}